package com.hangwei.paperless.utils;

import android.text.TextUtils;

import com.hangwei.paperless.adapter.accept.Item2Adapter;
import com.hangwei.paperless.model.bean.AcceptCheckInfo;
import com.hangwei.paperless.model.bean.BatchSheet;
import com.hangwei.paperless.model.bean.DRSInfo;
import com.hangwei.paperless.model.bean.DictBean;
import com.hangwei.paperless.model.bean.SubProcessListInfo;
import com.twelve.all.utils.LogUtil;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;

import java.util.List;

public class JsonPostUtil {
    private static void addKeyValue(StringBuilder builder, String key, Object value) {
        addKeyValue(builder, key, value, true);
    }

    /**
     * @param flag 拼接完的键值对是否追加 逗号
     */
    private static void addKeyValue(StringBuilder builder, String key, Object value, boolean flag) {
        if (value instanceof String && value != null) {
            builder.append(TAG_A).append(key).append(TAG_A).append(TAG_C).append(TAG_A).append(value).append(TAG_A);
        } else {
            builder.append(TAG_A).append(key).append(TAG_A).append(TAG_C).append(value);
        }
        if (flag)
            builder.append(TAG_B);
    }

    private static final String TAG_A = "\"";
    private static final String TAG_B = ",";
    private static final String TAG_C = ":";

    public static String buildAcceptJson(List<AcceptCheckInfo.TBean> list, String billId, int step) {

        StringBuilder builder = new StringBuilder();
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        builder.append("{");
        addKeyValue(builder, "userId", userId);
        addKeyValue(builder, "billId", billId);
        addKeyValue(builder, "billSeq", step);
        builder.append(TAG_A).append("recordList").append(TAG_A).append(TAG_C);
        builder.append("[");
        for (int i = 0; i < list.size(); i++) {
            AcceptCheckInfo.TBean bean = list.get(i);
            builder.append("{");
            addKeyValue(builder, "seq", bean.seq);
            addKeyValue(builder, "id", bean.id);
            addKeyValue(builder, "description", bean.description);
            addKeyValue(builder, "pn", bean.pn);
            addKeyValue(builder, "qty", bean.qty);
            addKeyValue(builder, "sn", bean.sn);
            addKeyValue(builder, "conditionId", bean.conditionId);
            if (bean.resultVal != null && bean.resultVal.equals(Item2Adapter.mEmptySelectText))
                bean.resultVal = null;
            addKeyValue(builder, "resultVal", bean.resultVal);
            addKeyValue(builder, "remark", bean.remark);
            builder.append(TAG_A).append("isAdd").append(TAG_A).append(TAG_C).append(bean.isAdd);
            builder.append("}");
            if (i != list.size() - 1)
                builder.append(TAG_B);
        }
        builder.append("]").append("}");
        String content = builder.toString();
        LogUtil.i("content:" + content);
        return content;
    }

    public static String buildRSSaveJson(String userId, String billId, String stepId, List<DRSInfo.DrsBean> drsList) {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        addKeyValue(builder, "userId", userId);
        addKeyValue(builder, "billId", billId);
        addKeyValue(builder, "stepId", stepId);
        builder.append(TAG_A).append("drsList").append(TAG_A).append(TAG_C).append("[");
        if (drsList != null)
            for (int i = 0; i < drsList.size(); i++) {
                DRSInfo.DrsBean drsBean = drsList.get(i);
                builder.append("{");
                addKeyValue(builder, "id", drsBean.id);
                addKeyValue(builder, "actual", drsBean.actual);
                addKeyValue(builder, "isNA", drsBean.isNA);
                if (drsBean.selectValue != null && drsBean.selectValue.size() > 0)
                    addKeyValue(builder, "selectValue", drsBean.selectValue.get(0));
                builder.append("}");
                if (i != drsList.size() - 1)
                    builder.append(TAG_B);
            }
        builder.append("]").append("}");
        String content = builder.toString();
        LogUtil.i("content:" + content);
        return content;
    }

    public static String buildSubProcessSave(String userId, String billId, String id, String pn) {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        addKeyValue(builder, "userId", userId);
        addKeyValue(builder, "billId", billId);
        addKeyValue(builder, "id", id);
        addKeyValue(builder, "pn", pn, false);
        builder.append("}");
        String content = builder.toString();
        LogUtil.i("content:" + content);
        return content;
    }

    public static String buildSaveLotData(String userId, String billId, List<BatchSheet> data) {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        addKeyValue(builder, "userId", userId);
        addKeyValue(builder, "billId", billId);
        builder.append("recordList").append(TAG_C).append("[");
        for (int i = 0; i < data.size(); i++) {
            BatchSheet batchSheet = data.get(i);
            builder.append("{");
            addKeyValue(builder, "id", batchSheet.id);
            addKeyValue(builder, "recdQty", batchSheet.recdQty);
            addKeyValue(builder, "remark", batchSheet.remark, false);
            builder.append("}");
            if (i != data.size() - 1)
                builder.append(TAG_B);
        }
        builder.append("]").append("}");
        String content = builder.toString();
        LogUtil.i("content:" + content);
        return content;
    }

    public static String buildProcessList(String userId, List<SubProcessListInfo> data, SubProcessListInfo item, String stampId, String id) {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        addKeyValue(builder, "userId", userId);
        addKeyValue(builder, "rowId", item.id);
        addKeyValue(builder, "actualDim", item.actualDim);
        addKeyValue(builder, "stampUserId", id);
        if ("Hole I.D.".equals(item.description)) {
            addKeyValue(builder, "stampId", stampId);
            builder.append(TAG_A).append("recordList").append(TAG_A).append(TAG_C).append("[");
            for (int i = 0; i < data.size(); i++) {
                SubProcessListInfo info = data.get(i);
                builder.append("{");
                addKeyValue(builder, "id", info.id);
                addKeyValue(builder, "dbpMin", info.dbpMin);
                addKeyValue(builder, "dbpMax", info.dbpMax, false);
                builder.append("}");
                if (i != data.size() - 1)
                    builder.append(TAG_B);
            }
            builder.append("]");
        } else {
            addKeyValue(builder, "stampId", stampId, false);
        }
        builder.append("}");
        String content = builder.toString();
        LogUtil.i("content:" + content);
        return content;
    }

    public static String buildEngJson(String billId, String stepId, String discrepancyTypeId, String description, String pn, String apn, String sn, String problemDescription, String disposition, List<DictBean> data) {
        String userId = SPUtil.getString(SPKey.Account.USER_ID);
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        addKeyValue(builder, "userId", userId);
        builder.append(TAG_A).append("info").append(TAG_A).append(TAG_C).append("{");
        addKeyValue(builder, "billId", billId);
        addKeyValue(builder, "stepId", stepId);
        addKeyValue(builder, "discrepancyTypeId", discrepancyTypeId);
        addKeyValue(builder, "description", description);
        addKeyValue(builder, "pn", pn);
        addKeyValue(builder, "apn", apn);
        addKeyValue(builder, "sn", sn);
        addKeyValue(builder, "problemDescription", problemDescription);
        addKeyValue(builder, "disposition", disposition);
        builder.append(TAG_A).append("discrepancyDescription").append(TAG_A).append(TAG_C).append("[");
        for (int i = 0; i < data.size(); i++) {
            DictBean bean = data.get(i);
            if (i == data.size() - 1) {
                if (TextUtils.isEmpty(bean.id))
                    builder.append(bean.id);
                else builder.append(TAG_A).append(bean.id).append(TAG_A);
            }else {
                if (TextUtils.isEmpty(bean.id))
                    builder.append(bean.id);
                else builder.append(TAG_A).append(bean.id).append(TAG_A);
                builder.append(TAG_B);
            }
        }
        builder.append("]").append("}");
        builder.append("}");

        String content = builder.toString();
        LogUtil.i("content:" + content);
        return content;
    }
}
